﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace ToyundaPhone.Database
{
	public partial class SongDatabase
	{
		public IWordExtractor WordExtractor { get; set; }

		partial void InsertSong(Song instance)
		{
			IndexSong(instance);
			ExecuteDynamicInsert(instance);
		}

		partial void UpdateSong(Song instance)
		{
			IndexSong(instance);
			ExecuteDynamicUpdate(instance);
		}

		protected void IndexSong(Song song)
		{
			if (WordExtractor != null)
			{
				song.SearchText = string.Join(string.Empty, WordExtractor.GetWords(song.Work).Union(WordExtractor.GetWords(song.Title)).Select(w => " " + w));
			}
			else
			{
				song.SearchText = " " + song.Work + " " + song.Title;
			}
		}
	}
}
